package com.aynu.gulimall.search.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

/**
 * @author mj
 * @version 1.0
 * @date 2021/3/7 14:07
 * <p>
 * 1、导入依赖
 * 2、编写配置，给容器注入一个RestHighLevelClient
 * 3、参照API
 */
@Configuration
public class ElasticSearchConfig {

    public static final RequestOptions COMMON_OPTIONS;

    static {
        RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
//        builder.addHeader("Authorization", "Bearer " + TOKEN);
//        builder.setHttpAsyncResponseConsumerFactory(
//                new HttpAsyncResponseConsumerFactory
//                        .HeapBufferedResponseConsumerFactory(30 * 1024 * 1024 * 1024));
        COMMON_OPTIONS = builder.build();
    }

    @Value("spring.elasticsearch.rest.uris")
    private String elasticSearchURL;

    @Bean
    public RestHighLevelClient esRestClient() {

        //初始化builder
        RestClientBuilder builder = null;

        //创建builder对象
        builder = RestClient.builder(HttpHost.create(elasticSearchURL));

        //获取client对象
        RestHighLevelClient client = new RestHighLevelClient(builder);
        return client;
    }
}
